package com.example.xunua_weather.Sqliteutils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class DBManager {
    public static SQLiteDatabase database;

    public static void initDB(Context context){
        MySqliteHelper mySqliteHelper=new MySqliteHelper(context);
        database=mySqliteHelper.getWritableDatabase();
    }

    //查找数据库中的所有的城市名字
    public static List<String>queryAllCityName(){
        Cursor cursor = database.query("info1", null, null, null, null,null,null);List<String>cityList=new ArrayList<>();
        cursor.moveToFirst();
        if (cursor.getCount() != 0) {
            do {
                String city=cursor.getString(cursor.getColumnIndex("city"));
                cityList.add(city);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return cityList;
    }

    //根据城市名，替换信息内容
    public static int updateInfoBycity(String city,String content){
        ContentValues values=new ContentValues();
        values.put("content",content);
        return database.update("info1",values,"city=?",new String[]{city});
    }

    //添加数据
    public static long addCityInfo(String city,String content){
        ContentValues values=new ContentValues();
        values.put("city",city);
        values.put("content",content);
        return database.insert("info1",null,values);
    }

    //查询数据库中全部信息
    public static List<DatabaseBean>queryAllInfo(){
        Cursor cursor=database.query("info1",null,null,null,null,null,null);
        List<DatabaseBean> mDatas=new ArrayList<>();
        cursor.moveToFirst();
        if (cursor.getCount() != 0) {
            do {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String city = cursor.getString(cursor.getColumnIndex("city"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                DatabaseBean bean = new DatabaseBean(id, city, content);
                mDatas.add(bean);
            } while (cursor.moveToNext());
        }
        return mDatas;
    }

    //存储地点天气最多存储五个，一旦超过五个就不存储，获取目前的存储数量
    public static int getCityCount(){
        Cursor cursor=database.query("info1",null,null,null,null,null,null);
        int count = cursor.getCount();
        return count;
    }

    //传入城市名 删除当前城市在数据库中的数据
    public static int deleteInfoBycity(String city){
        return database.delete("info1","city=?",new String[]{city});
    }

    //传入城市名，查询数据库当中的内容
    public static String queryInfoByCity(String city){
        Cursor cursor=database.query("info1",null,null,null,null,null,null);
        if (cursor.getCount()>0){
            cursor.moveToFirst();
            String content=cursor.getString(cursor.getColumnIndex("content"));
            return content;
        }
        return null;
    }

    //删除表当中所有数据信息
    public static void deleteAllInfo(){
        String sql="delete from info1";
        database.execSQL(sql);
    }
}
